<?php

namespace app\company\controller\statistics\crisis_intervention;

use app\admin\service\InterfereExportService;
use app\BaseController;
use app\model\CrisisBehaviorModel;
use app\model\CrisisTypeModel;
use app\model\FollowUserModel;
use app\model\InterfereBehaviorModel;
use app\model\InterfereUserModel;
use app\model\UserModel;
use think\App;

class MonthlyReport extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 本期新增数据-月报
     * @return bool|string
     */
    public function MonthAddData(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'unit_id|单位ID' => 'require',
                'date|时间' => 'require'
            ]);
            $start_time = strtotime($param['date'] . '-01');
            $end_time = strtotime(date('Y-m-t 23:59:59', $start_time));
            $InterfereUserModel = new InterfereUserModel();
            $FollowUserModel = new FollowUserModel();

            //新增待干预
            $uids = $InterfereUserModel->where([
                ['unit_id','=',$param['unit_id']],
                ['move_status','=',0],
                ['time','between',[$start_time, $end_time]]
            ])->column('uid');
            $uids = array_unique($uids);
            $NewInterfereNum = count($uids);
            //新增关注
            $uids = $FollowUserModel->where('time','between',[$start_time, $end_time])->column('uid');
            $uids = array_unique($uids);
            $NewFollowNum = count($uids);
            //已结束干预
            $uids = $InterfereUserModel->where([
                ['unit_id','=',$param['unit_id']],
                ['status','=',1],
                ['time','between',[$start_time, $end_time]]
            ])->column('uid');
            $uids = array_unique($uids);
            $NewInterfereNum2 = count($uids);
            //待处理干预
            $uids = $InterfereUserModel->where([
                ['unit_id','=',$param['unit_id']],
                ['status','=',0],
                ['move_status','=',1],
                ['time','between',[$start_time, $end_time]]
            ])->column('uid');
            $uids = array_unique($uids);
            $NewInterfereNum3 = count($uids);
            return success([
                'new_interfere_num' => $NewInterfereNum,
                'new_interfere_num2' => $NewInterfereNum2,
                'new_interfere_num3' => $NewInterfereNum3,
                'new_follow_num' => $NewFollowNum,
            ]);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 本月干预人员中男女比例-月报
     * @return bool|string
     */
    public function MonthSexRatio(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'unit_id|单位ID' => 'require',
                'date|时间' => 'require'
            ]);
            $start_time = strtotime($param['date'] . '-01');
            $end_time = strtotime(date('Y-m-t 23:59:59', $start_time));
            $InterfereUserModel = new InterfereUserModel();
            $UserModel = new UserModel();
            $uids = $InterfereUserModel->where([
                ['unit_id','=',$param['unit_id']],
                ['time','between',[$start_time, $end_time]]
            ])->column('uid');
            $uids = array_unique($uids);
            $data['boy'] = $UserModel->where([
                ['id','in',$uids],
                ['sex','=',1]
            ])->count();
            $data['girl'] = $UserModel->where([
                ['id','in',$uids],
                ['sex','=',2]
            ])->count();
            $data['total'] = $data['boy'] + $data['girl'];
            return success($data);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 本期异常情况统计-月报
     * @return bool|string
     */
    public function MonthException(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'unit_id|单位ID' => 'require',
                'date|时间' => 'require'
            ]);
            $start_time = strtotime($param['date'] . '-01');
            $end_time = strtotime(date('Y-m-t 23:59:59', $start_time));
            $InterfereUserModel = new InterfereUserModel();
            $InterfereBehaviorModel = new InterfereBehaviorModel();
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            $Behavior = $CrisisBehaviorModel->where([
                ['unit_id','=',$param['unit_id']],
                ['status','=',1]
            ])->select()->toArray();
            $data = [];
            foreach ($Behavior as $value){
                $data['name'][] = $value['name'];
                $interfere_id = $InterfereBehaviorModel->where('behavior_id','=',$value['id'])->column('interfere_id');
                $interfere_id = array_unique($interfere_id);
                $uids = $InterfereUserModel->where([
                    ['id','in',$interfere_id],
                    ['time','between',[$start_time,$end_time]]
                ])->column('uid');
                $uids = array_unique($uids);
                $data['num'][] = count($uids);
            }
            return success($data);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 疑似问题类型选择器
     * @return bool|string
     */
    public function CrisisTypeSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'unit_id|单位ID' => 'require'
            ]);
            $where = [];
            $where[] = ['unit_id', '=', $param['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisTypeModel = new CrisisTypeModel();
            $data = $CrisisTypeModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 危机干预月报导出
     * @return bool|string
     */
    public function MonthlyReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'unit_id|单位ID' => 'require'
            ]);
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->MonthlyReport($param, [
                'unit_id' => $param['unit_id'],
            ]);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}